package de.ts.fremdsprache;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.ts.database.DAO;

public class Controller extends HttpServlet {

	public Controller() {
		super();
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -246998949059099292L;

	public static final String[] PARAM_NAMES = { "login", "password",
			"Vorname", "Nachname", "Geburtsdatum" };

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		RequestDispatcher rd;
		HttpSession session = request.getSession(true);

		boolean error = false;

		int paramCnt = PARAM_NAMES.length;
		Map<String, String> params = new HashMap<String, String>();
		String param;
		int i = 0;
		while (i < paramCnt) {
			if ((param = request.getParameter(PARAM_NAMES[i])) != null) {
				params.put(PARAM_NAMES[i], param);
				session.setAttribute(PARAM_NAMES[i], params.get(PARAM_NAMES[i]));
				// Eingabeparameter wurde neu gesetzt
			} else {
				if ((param = (String) session.getAttribute(PARAM_NAMES[i])) != null) {
					params.put(PARAM_NAMES[i], param);
					// Eingabeparameter wurde der Session entnommen
				} else {
					error = true;
					break;
					// kein Eingabeparameter verfuegbar
				}
			}
			i++;

		}

		boolean validLogin = false;
		String loginName;
		String pwForGivenLogin;
		loginName = params.get("login");
		if (loginName != null) {
			pwForGivenLogin = loginName + "pw";
			if (pwForGivenLogin.equals(params.get("password"))) {
				validLogin = true;
			}
		}

		if (!validLogin) {
			rd = request.getRequestDispatcher("/anmeldung.jsp");
			rd.forward(request, response);
			return;

		}

		

		DAO dao = new DAO("database.sql");

		if (error) {
			boolean isAdmin = false; // dao.isAdmin(loginName)
			if (isAdmin) {
				rd = request.getRequestDispatcher("/administration.jsp");
				request.setAttribute("users", dao.getUsers());
				rd.forward(request, response);
			} else {
				rd = request.getRequestDispatcher("/fremdsprachenAnm.jsp");
				rd.forward(request, response);
			}
		}

		
		String birthday = params.get("Geburtsdatum");
		birthday = birthday; // wandle den String in datenbankkonformen DATETIME
								// String oder Aehnliches um - evtl error auf
								// true setzen
		params.put("Geburtsdatum", birthday);

		// evtl weitere pruefungen der Eingabedaten

		String education;
		/*
		 * new User(params.get("Vorname"),
		 * params.get("Nachname"),params.get("Geburtsdatum"), gender, school,
		 * education, testType, birthplace, grade, postalnumber, city, address,
		 * phonenumber, email, faxnumber, companyName, companyAddress,
		 * companyFax, companyPostalnumber, companyCity, companyContact,
		 * companyPhonenumber); dao.insertUser(user);
		 */
		

		rd = request.getRequestDispatcher("/erfolg.jsp");
		rd.forward(request, response);
		
	}
	
}